package com.yonyou.crm.cum.customer.appctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.cum.customer.rmitf.ICustomerRmService;
import com.yonyou.crm.sys.dynamic.entity.DynamicVO;
import com.yonyou.crm.sys.dynamic.rmitf.IDynamicRmService;
import com.yonyou.crm.sys.modules.entity.MtObjTypeEnum;

@RestController
@RequestMapping(value = "/{version}/mcum/customers")
public class AppCustomerDynamicCtrl {
	
	@Autowired
	private IDynamicRmService service;
	@Autowired
	private ICustomerRmService customerService;
	
	/**
	 * 根据客户ID获取客户动态列表
	 * @param version
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}/dynamics", method = RequestMethod.GET)
	public Object getCustomerDymList(@PathVariable("version") String version, @PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		DynamicVO[] dymArr = customerService.getCumDynamic(id);
		return ResponseUtil.getObject(transDymList2Json(dymArr));
	}
	
	/**
	 * 将返回结果转化为移动端所需数据
	 * @param dymList
	 * @return
	 */
	private JSONObject transDymList2Json(DynamicVO[] dymArr){
		JSONObject result = new JSONObject();
		if(dymArr == null || dymArr.length == 0) return result;
		List<Map<String, Object>> dynamiclist = new ArrayList<Map<String,Object>>();
		for(DynamicVO vo : dymArr){
			Map<String, Object> voMap = new HashMap<String, Object>();
			voMap.put("id", vo.getId());
			voMap.put("type", vo.getType());
			voMap.put("time", vo.getFormatTime());
			voMap.put("name", vo.getOwnerName());
			voMap.put("userid", vo.getOwnerId());
			voMap.put("content", vo.getContent());
			dynamiclist.add(voMap);
		}
		result.put("dynamiclist", dynamiclist);
		return result;
	}
}
